package zhongbo.java.air.flightpath.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.flightpath.listener.RedisLogChanelPublisher;
import zhongbo.java.air.flightpath.model.FlightPath;
import zhongbo.java.air.flightpath.service.FlightPathService;

@RestController
@RequestMapping("/flightPath")
@Api(tags = "航迹相关接口")
public class FlightPathController {

    @Autowired
    private FlightPathService flightPathService;

    @Autowired
    private RedisLogChanelPublisher redisLogChanelPublisher;

    @GetMapping("/findPage")
    @ApiOperation("分页查询")
    public ResultVo<PageVo<FlightPath>> page(Integer page, Integer pageSize
            , String icao, String startTime, String endTime
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc){
        return ResultVo.ok(flightPathService.pageQuery(page, pageSize
                , icao, startTime, endTime
                , keyword, fields, oderByField, orderByAsc));
    }

    @PostMapping("/add")
    @ApiOperation("添加航迹信息")
    public ResultVo saveAirCraft(@RequestBody FlightPath entity){
        return flightPathService.saveAirCraft(entity);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除航迹信息")
    public ResultVo deleteAirCraft(String entityId){
        if (!flightPathService.removeById(entityId)){
            return ResultVo.error("删除失败");
        }

        redisLogChanelPublisher.broadcast("id为" + entityId + "的航迹已被删除");
        return ResultVo.ok("删除id:" + entityId);
    }

    @PutMapping("/edit")
    @ApiOperation("修改航迹信息")
    public ResultVo updateAirCraft(@RequestBody FlightPath edit){
        return flightPathService.updateAirCraft(edit);
    }

    @GetMapping("findOne")
    @ApiOperation("查询一条")
    ResultVo<FlightPath> findOne(String entityId) {
        FlightPath flightPath = flightPathService.getById(entityId);
        return ResultVo.ok(flightPath);
    }
}
